﻿using Autofac;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;

namespace ZoneLine.Stations
{
    public class ProductIndexHelper
    {
        private static bool _isFirstEnter = true;

        public static int GetIndex(ModelEnum model, short baseIndex, WorkDirectionEnum LF,WorkDirectionEnum stationLF, bool needInc = true)
        {
            var dt = DateTime.Now;
            var dal = ContainerServer.Container.Resolve<IModelDAL>();
            var days = (dt - DateTime.Parse("2020-01-01")).Days;

            if (_isFirstEnter)
            {
                dal.DeleteModel<PieceIndexModel>(p => p.DayIndex < days - 200);
                _isFirstEnter = false;
            }

            var pieceModel = dal.GetModel<PieceIndexModel>(p => p.DayIndex == days && p.Model == model && p.LF == LF&&p.StationLF==stationLF);

            if (pieceModel == null)
            {
                pieceModel = new PieceIndexModel();
                pieceModel.DayIndex = days;
                pieceModel.Model = model;
                pieceModel.LF = LF;
                pieceModel.StationLF = stationLF;
                pieceModel.Index = 1;
                dal.AddModel(pieceModel);
                return pieceModel.Index + baseIndex;
            }
            else
            {
                if (needInc)
                {
                    pieceModel.Index++;
                }
                dal.UpdateModelSuper<PieceIndexModel>(p => p.Id == pieceModel.Id, pieceModel);
                return pieceModel.Index + baseIndex;
            }
        }
    }
}
